package com.skytech.tax.customsdatawarehouse.singlewindow.taxhandle.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.taxhandle.domain.entity.TaxBillCheckListFile;
import org.joda.time.DateTime;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author Lianhong
 * @since 2021/11/30
 */
@Repository
public class TaxBillCheckListFileDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String selectCheckListFileSql = "SELECT file FROM sw_tax_bill_chk_list_file WHERE taxHeadSeqNo = ?";

    public String findByTaxHeadSeqNo(String taxHeadSeqNo) {
        return namedParameterJdbcTemplate.getJdbcOperations().queryForObject(selectCheckListFileSql,
                new Object[]{taxHeadSeqNo}, String.class);
    }

    private String insertCheckListFileSql = "INSERT INTO sw_tax_bill_chk_list_file (taxHeadSeqNo, file, storageTime) VALUES (:taxHeadSeqNo, :file, :storageTime)";

    public void save(TaxBillCheckListFile checkListFile) {
        checkListFile.setStorageTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        this.namedParameterJdbcTemplate.update(this.insertCheckListFileSql, new BeanPropertySqlParameterSource(checkListFile));
    }

    private String existsCheckListFileSql = "SELECT count(1) FROM sw_tax_bill_chk_list_file WHERE taxHeadSeqNo = ?";

    public boolean exists(String taxHeadSeqNo) {
        Integer count = namedParameterJdbcTemplate.getJdbcOperations()
                .queryForObject(this.existsCheckListFileSql, new Object[]{taxHeadSeqNo}, Integer.class);
        return count != null && count > 0;
    }
}
